<?php
session_start();
require_once("../config/include_tokyo.inc.php");
require_once("../class/Core_Lib_Page.php");


//访问权限验证
$userRole = $_SESSION['user_role'];
if($userRole == 1){
	//管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}

//任务操作
$doFlag = $_POST["doFlag"];
$searchKeyword = $_POST["search_keyword"];
if($doFlag == "addTask"){
	//新加任务
	$taskContent = $_POST["taskContent"];
	$taskLevel = $_POST["taskLevel"];
	$sql = "INSERT INTO tasks (content,level,status) VALUES ('$taskContent','$taskLevel',1)";
	$db->Execute($sql);
}else if($doFlag == "addProcess"){
	//追加任务进展
	$taskId = $_POST["taskId"];
	$processStr = $_POST["process_".$taskId];
	$sql = "insert into taskprocess(task_id,task_process) values ($taskId,'$processStr')";
	$db->Execute($sql);
}else if($doFlag == "doComplete"){
	//任务完成
	$taskId = $_POST["taskId"];
	$sql = "update tasks set status=2 where id=".$taskId;
	$db->Execute($sql);
	$processStr = "完成时间";
	$sql = "insert into taskprocess(task_id,task_process) values ($taskId,'$processStr')";
	$db->Execute($sql);
}else if($doFlag == "doDelete"){
	//删除任务
	$taskId = $_POST["taskId"];
	$sql = "update tasks set status=3 where id=".$taskId;
	$db->Execute($sql);
	$processStr = "删除时间";
	$sql = "insert into taskprocess(task_id,task_process) values ($taskId,'$processStr')";
	$db->Execute($sql);
}	



//显示任务列表 & 分页设置
$listFlag = $_POST["listFlag"];
if($listFlag == ""){
	$listFlag = "1";
}
$list_rows = 50; //每页显示的记录数
$sql_all = "SELECT COUNT(*) as total_rows from tasks t where t.status=1 "; //default 处理中任务
if($listFlag == "2"){ //全部任务
	$sql_all = "SELECT COUNT(*) as total_rows from tasks t  ";
}else if($listFlag == "4"){ //任务搜索
	$sql_all = "SELECT  COUNT(*) as total_rows from tasks t where t.content like '%$searchKeyword%'";
}
$result = $db->Execute($sql_all);
while (!$result->EOF) {
	$totalRows = $result->fields['total_rows'];
	$result->MoveNext();
}
$nowPage = $_POST['nowPage'];
$nowPage = !empty($nowPage) ? $nowPage : 1;
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => $nowPage 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;





$sql = "SELECT * from tasks t where t.status=1  order by modifytime desc limit $limit_m,$limit_n"; //default 处理中任务
if($doFlag == "toList"){
	if($listFlag == "1"){ //处理中
		$sql = "SELECT * from tasks t where t.status=1  order by modifytime desc limit $limit_m,$limit_n";
	}else if($listFlag == "2"){ //全部任务
		$sql = "SELECT * from tasks t   order by modifytime desc limit $limit_m,$limit_n";
	}else if($listFlag == "3"){ //清理ok,任务进展最后一条是ok的，该任务标记为 已完成；
		$sql_1 = "SELECT * from tasks t where t.status=1  order by modifytime desc "; //只准对 处理中任务
		$result = $db->Execute($sql_1);
		while (!$result->EOF) {
			$sql_2 = "SELECT * from taskprocess where task_id=".$result->fields['id']." order by modifytime asc";
			$resultProcess = $db->Execute($sql_2);
			$isOK = false;
			while (!$resultProcess->EOF) {
				$task_processField = $resultProcess->fields['task_process'];
				if($task_processField == "ok" || $task_processField == "OK"){
					$isOK = true;
				}else{
					$isOK = false;
				}
				$resultProcess->MoveNext();	
			}
			if($isOK ){//进展记录最后一条是"ok"
					//任务完成
					$taskId = $result->fields['id'];
					$sql_3 = "update tasks set status=2 where id=".$taskId;
					$db->Execute($sql_3);
					$processStr = "自动清理ok";
					$sql_4 = "insert into taskprocess(task_id,task_process) values ($taskId,'$processStr')";
					$db->Execute($sql_4);				
			}
			$result->MoveNext();	
		}
	}else if($listFlag == "4"){ //任务搜索
		$sql = "SELECT * from tasks t where t.content like '%$searchKeyword%' order by modifytime desc limit $limit_m,$limit_n "; 		
	}
}

$result = $db->Execute($sql);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "任务列表"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="listForm" action="../task/taskList.php" method="post">
<div id="container" >	

<div id="newTask">
<div class="leftDiv">
<textarea name="taskContent" id="taskContent" rows=3 cols=50></textarea><br>
等级：
<select name="taskLevel" id="taskLevel">
<option value=1 selected>普通</option>
<option value=2>重要</option>
</select>
<br>
<span style="margin-left:280px;">
<a href="javascript:addTask()" class="button icon add">新加任务</a>
</span>
</div>

<div class="leftDiv left2">
<textarea name="search_keyword" id="search_keyword" rows=3 cols=50></textarea><br><br>
<span style="margin-left:280px;">
<a href="javascript:toList(4)" class="button icon search">搜索任务</a>
</span>

</div>
<div class="clearBoth">&nbsp;</div>




<div class="grayTable">	
<?php
$toList_1_class = "";
$toList_2_class = "";

if($listFlag == "1"){
	$toList_1_class = "class='currentLink'";
}else if($listFlag == "2"){
	$toList_2_class = "class='currentLink'";
}
?>
<caption>
<span <?php echo $toList_1_class;?> ><a href="javascript:toList(1)">处理中</a></span>
<span <?php echo $toList_2_class;?> ><a href="javascript:toList(2)">全部任务</a></span>
<span  ><a href="javascript:toList(3)">清理OK</a></span>
</caption>
<br>
<table >

<thead>
	<tr>
		<th scope="col" width=70>时间</th>
	    <th scope="col" >任务</th>
		<th scope="col" >进展</th>
		<th scope="col" width=95>操作</th>
	</tr>
</thead>

<tbody>	
<?php
	if(empty($result) || $result->RecordCount() < 1){
		echo "<tr><td colspan=4 align='center'>记录为空.</td></tr>";
	}else{		
		while (!$result->EOF) {
			//取当前任务的进展描述
			$sql = "SELECT * from taskprocess where task_id=".$result->fields['id']." order by modifytime asc";		
			$resultProcess = $db->Execute($sql);
			$currentProcess = "";
			while (!$resultProcess->EOF) {
				$task_processField = $resultProcess->fields['task_process'];
				if(strlen($task_processField)>100){
					$task_processField = substr($task_processField,0,100)."...";
				}
				$currentProcess = $currentProcess.$resultProcess->fields['modifytime']."&nbsp;&nbsp;".$task_processField."<br>";
				$resultProcess->MoveNext();	
			}
			
					
			
			$task_contentField = $result->fields['content'];			

			if($result->fields['level'] == 2){
				$strTR = "<tr class='important'>"; //重要任务
			}else{
				$strTR = "<tr >";
			}		
			$strTR = $strTR."<td width=70>".$result->fields['modifytime']."</td>"."<td  >".$task_contentField."</td>";
			$strTR = $strTR."<td>".$currentProcess."<input type='text' size=30 name='process_".$result->fields['id']."' id='process_".$result->fields['id']."'><input type='button' value='提交' onclick='addProcess(".$result->fields['id'].")'></td>";
			if($result->fields['status'] == 1){
				$strTR = $strTR."<td width=95><input type='button' value='完成' onclick='doComplete(".$result->fields['id'].")'><input type='button' value='删除' onclick='doDelete(".$result->fields['id'].")'></td>";
			}else if($result->fields['status'] == 2){
				$strTR = $strTR."<td>已完成</td>";
			}else if($result->fields['status'] == 3){
				$strTR = $strTR."<td>已删除</td>";
			}
			
			$strTR = $strTR."</tr>";
			echo $strTR;
			$result->MoveNext();	
		}
	}

?>
</tbody>
					
</table>	
<br>

<!--Page 分页 -->
<div id="page"><?php echo $page->show(2);?></div>

</div>



</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" value="<?php echo $doFlag; ?>">
<input type="hidden" name="listFlag" id="listFlag" value="<?php echo $listFlag; ?>">
<input type="hidden" name="taskId" id="taskId" value="<?php echo $taskId; ?>">
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>">
</form>
<br><br><br>
</body>




</html>

<script>

<?php
$doFlag = $_POST["doFlag"];
if($doFlag == "addProcess"){
	$taskId = $_POST["taskId"];
	echo "$('#process_".$taskId."').focus(); "; 
}else{
	echo "$('#taskContent').focus(); "; 
}
?>


function addTask(){
	document.getElementById('nowPage').value = 1;//第一页
	var taskContent = $("#taskContent").val();	
	if(taskContent==""){
		alert("任务内容不能为空");
		$("#taskContent").focus();  
		return;
	}else{
		document.getElementById('doFlag').value = "addTask";
		document.listForm.submit();
	}
}


function addProcess(id){
	document.getElementById('doFlag').value = "addProcess";
	document.getElementById('taskId').value = id;
	var processStr = document.getElementById('process_'+id).value;
	if(processStr == ""){
		alert("进展内容不能为空")
		document.getElementById('process_'+id).focus();  
		return;
	}
	document.listForm.submit();
}


function doComplete(id){
	document.getElementById('doFlag').value = "doComplete";
	document.getElementById('taskId').value = id;
	document.listForm.submit();
}


function doDelete(id){
	document.getElementById('doFlag').value = "doDelete";
	document.getElementById('taskId').value = id;
	document.listForm.submit();
}


function toList(listFlag){
	document.getElementById('nowPage').value = 1;//第一页
	document.getElementById('doFlag').value = "toList";
	document.getElementById('listFlag').value = listFlag;
	document.listForm.submit();
}


function goToPage(nowPage){
	document.getElementById('doFlag').value = 'toList';
	document.getElementById('nowPage').value = nowPage;
	document.listForm.submit();
}

</script>

